package io.renren.modules.hos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.hos.service.DoctorService;
import io.renren.modules.hos.utils.PageParamsUtils;
import io.renren.modules.hos.vo.DoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hos.dao.CollDoctorDao;
import io.renren.modules.hos.entity.CollDoctorEntity;
import io.renren.modules.hos.service.CollDoctorService;


@Service("collDoctorService")
public class CollDoctorServiceImpl extends ServiceImpl<CollDoctorDao, CollDoctorEntity> implements CollDoctorService {
    @Autowired
    private DoctorService doctorService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CollDoctorEntity> page = this.page(
                new Query<CollDoctorEntity>().getPage(params),
                new QueryWrapper<CollDoctorEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public DoctorVo selectDoctorById(Long doctorId) {
        return doctorService.selectDoctorById(doctorId);
    }

    @Override
    public boolean addCollDoctor(Long doctorId) {
        CollDoctorEntity collDoctorEntity = new CollDoctorEntity();
        collDoctorEntity.setDoctorId(doctorId);
        collDoctorEntity.setUserId(ShiroUtils.getHosUserId());
        collDoctorEntity.setCollDate(new Date());

        return this.save(collDoctorEntity);
    }

    @Override
    public boolean cancel(Long doctorId) {
        LambdaQueryWrapper<CollDoctorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CollDoctorEntity::getDoctorId, doctorId);
        queryWrapper.eq(CollDoctorEntity::getUserId, ShiroUtils.getHosUserId());

        return this.remove(queryWrapper);
    }

    @Override
    public PageUtils selectUserColl(Map<String, Object> params) {
        List<DoctorVo> doctorVoList = baseMapper.selectUserColl(ShiroUtils.getHosUserId());
        int pageSize = PageParamsUtils.getSize(params);
        int pageIndex = PageParamsUtils.getIndex(params);
        return new PageUtils(doctorVoList, doctorVoList.size(), pageSize, pageIndex);
    }

}